<!DOCTYPE html>

<meta charset="utf-8" />	

<style type="text/css">
.table-header {
    background-color: #46A3FF;
    color: #fff;
    font-size: 14px;
    line-height: 38px;
    margin-bottom: 1px;
    padding-left: 12px;
}
.search_panel {
    height: 50px;
}
</style>


<div class="search_panel">
	<div class="widget-main">
		<form action="" method="post">
			<div class="col-sm-6">
				<div class="search_elm" style="margin-left: 0px">
					<label class="label_search">合作商:</label>
					<div class="search_input">
						<input id="agentId" type="hidden" name="agentId" />
						<input id="agentName" type="text" name="agentName"  readonly="readonly"/>
					</div>
				</div>
			</div>
		</form>
		
		<div class="col-sm-6">
			<div class="search_elm" style="margin-left: 10px;width: 160px;">
				<label class="label_search">机构号:</label>
				<div class="search_input">
					<input id="search_cooporgNo" type="text" name="cooporgNo" class="col-xs-4 col-sm-8" placeholder="合作机构号"/>
				</div>
			</div>
			<div class="search_elm" style="margin-left: 10px;width: 130px;">
				<label class="label_search">类型:</label>
				<div class="search_input">
					<select 
						 rel="obj.RATETYPE"
			 			 location="local" 
						 name="rateType" 
			 			id="search_rateType"
			 			data-placeholder="请选择状态" style="width: 90px;vertical-align:middle;">
						<option value='' >-请选择-</option>
					</select>
				</div>
			</div>
			<div class="search_elm" style="margin-left: 10px;width: 120px;">
				<label class="label_search">封顶:</label>
				<div class="search_input">
					<input id="search_rateTypeTop" type="text" name="rateTypeTop" class="col-xs-4 col-sm-8" placeholder="封顶金额"/>
				</div>
			</div>
				<button style="margin-left: 20px;" class="btn btn-small btn_search" onclick="search_Cooporg()" title="搜索" > 搜索 </button>
		</div>
	</div>
</div>

<div class="rowt">
	<div class="col-xs-6">
		<div class="table-header">
			已选大商户号
		</div>
		<div>
			<table id="sample-table-1" class="table table-striped table-bordered table-hover">
				<thead>
					<tr>
						<th class="center">
							<label class="position-relative">
								<input type="checkbox" class="ace" id="checkboxlbl1"/>
								<span class="lbl"></span>
							</label>
						</th>
						<th>合作机构号</th>
						<th>大商户号</th>
						<th>费率类型</th>
					</tr>
				</thead>

				<tbody id="mybody1">
				</tbody>
			
			</table>
		</div>
		<button class="btn btn-white btn-warning btn-bold" data-dismiss="modal" onclick="deleteAgentOrgMer();">
			<i class="ace-icon fa fa-trash-o bigger-120 orange"></i>移除
		</button>
		<ul class="pagination pull-right no-margin" id="paging_bar_1"></ul>
	</div>
	
	<div class="col-xs-6">
		<div class="table-header">
			待选大商户号
		</div>
		<div>
			<table id="sample-table-2" class="table table-striped table-bordered table-hover">
				<thead>
					<tr>
						<th class="center">
							<label class="position-relative">
								<input type="checkbox" class="ace" id="checkboxlbl2"/>
								<span class="lbl"></span>
							</label>
						</th>
						<th>合作机构号</th>
						<th>大商户号</th>
						<th>费率类型</th>
					</tr>
				</thead>

				<tbody id="mybody2">
				</tbody>
			</table>
		</div>
		<button class="btn btn-white btn-info btn-bold" data-dismiss="modal" onclick="saveAgentOrgMer()">
			<i class="ace-icon fa fa-floppy-o bigger-120 blue"></i>添加
		</button>
		<ul class="pagination pull-right no-margin" id="paging_bar_2"></ul>
	</div>
	
	
</div>
									
<br/><br/>
<div class="form-actions align-right  form-button-box" style="margin-top: 10px">
	<button class="btn" type="button" onclick="javascript:dialog.closeById('dlg-editeAgentOrg');">关闭</button>
</div>
<script type="text/javascript">
var pageSize=5;
var bodycp1=0;
var bodycp2=0;
$(document).ready(function() {
	$("#checkboxlbl1").change(function(){  
		var is_checked =$("#checkboxlbl1").is(":checked");
		$("input[name='mybody1paging_bar_1']").each(function(){
			this.checked=is_checked;
		}); 
	});
	
	$("#checkboxlbl2").change(function(){  
		var is_checked =$("#checkboxlbl2").is(":checked");
		$("input[name='mybody2paging_bar_2']").each(function(){
			this.checked=is_checked;
		}); 
	});
	
	var time=setInterval(function(){
		if($("#agentId").val()!=''){
			clearInterval(time);
			getjson("mpamng/agent/queryAgentOrgList.do",1,"mybody1","paging_bar_1");
			getjson("mpcoop/cooporgmer/queryCoopOrgList.do",1,"mybody2","paging_bar_2");
		}
	},100);
});

function search_Cooporg(){
	var url='mpcoop/cooporgmer/queryCoopOrgList.do?cooporgNo='+$("#search_cooporgNo").val()+'&rateType='+$("#search_rateType").val()+'&rateTypeTop='+$("#search_rateTypeTop").val();
	getjson(url,1,"mybody2","paging_bar_2");
}


function getjson(url,cp,body,bar){
	 $.ajax({
		 type:"post",
		 dataType:"json",
		 url:url,
		 data: {pageSize:pageSize,currentPages:cp,agentId:$("#agentId").val()},
		 success : function(result) {
				if (result.rspcod != 200) {
					msg.alert("错误", result.rspmsg + " 错误代码："+ result.rspcod, 'error');
				} else {
					if(body=='mybody1'){
						if(bodycp1!=0){
							$("#"+body+" tr").remove();
							$("#"+bar+" a").remove();
						}
					}else{
						if(bodycp2!=0){
							$("#"+body+" tr").remove();
							$("#"+bar+" a").remove();
						}
					}
					var pagecount = Math.ceil(result.records/pageSize);
					
					var paging_bar='<li class="prev '+(cp<=1?'disabled':'')+'"><a href="javascript:void(0)" onclick=\'getjson(\"'+url+'\",1,\"'+body+'\",\"'+bar+'\");\'>首页</a>';
					for(var i=1;i<=pagecount;i++){
						paging_bar+='<li class="'+(cp==i?'active':'')+'"><a href="javascript:void(0)" onclick=\'getjson(\"'+url+'\",'+i+',\"'+body+'\",\"'+bar+'\");\'>'+i+'</a>';
					}
					paging_bar+='<li class="next '+(cp==pagecount||pagecount==0?'disabled':'')+'"><a href="javascript:void(0)" onclick=\'getjson(\"'+url+'\",'+pagecount+',\"'+body+'\",\"'+bar+'\");\'>尾页</a>';
					$("#"+bar).append(paging_bar);
					
					if(result.obj==null||result.obj=='null'){
						return false;
					}
					$.each(jQuery.parseJSON(result.obj),function(index,obj){
						var inputVal="";
						if(body=='mybody1'){
							inputVal=obj.ageorgId;
						}else{
							inputVal=obj.cooporgNo+'#'+obj.merNo;
						}
						var tr ='<tr><td class="center"><label class="position-relative"><input value="'+inputVal+'" type="checkbox" name="'+(body+bar)+'" class="ace"  /><span class="lbl"></span></label></td>'
						+'<td>'+obj.cooporgNo+'</td><td>'+obj.merNo+'</td><td>'+(DICT.get("RATETYPE")[obj.rateType]||'')+((obj.rateTypeTop==0||obj.rateTypeTop==null||obj.rateTypeTop.length<=0)?'':'  - '+cent2Yuan(obj.rateTypeTop)+' 封顶')+'</td></tr>';
						$("#"+body).append(tr);
					});
					bodycp1=cp;
					bodycp2=cp;
				}
			},
		 error : function(XMLHttpRequest, textStatus) {
				msg.alert("错误", "错误代码：" + XMLHttpRequest.status + ",错误描述："+ textStatus, 'error');
			}
	 });
}
/**
 * onclick="selectLine(this)"
 
function selectLine(obj){
	var ck =$(obj).find(":checkbox").eq(0);
	$(ck).prop("checked", !($(ck).is(":checked")));
}
*/


function deleteAgentOrgMer(){
	var agentOrgMer="";
	$("input[name='mybody1paging_bar_1']").each(function(){
		if(this.checked==true){
			agentOrgMer+=$(this).val()+",";
		}
	});
	agentOrgMer = agentOrgMer.substring(0, agentOrgMer.lastIndexOf(","));
	if(agentOrgMer.length<=0){
		alertMsg.error('请选择删除的大商户号');
		return false;
	}
	$.ajax({
		 type:"post",
		 dataType:"json",
		 async:false,
		 url:"mpamng/agent/deleteAgentOrg.do",
		 data: {ageorgId:agentOrgMer},
		 success : function(result) {
				if (result.rspcod != 200) {
					msg.alert("错误", result.rspmsg + " 错误代码："+ result.rspcod, 'error');
				} else {
					getjson("mpamng/agent/queryAgentOrgList.do",1,"mybody1","paging_bar_1");
					getjson("mpcoop/cooporgmer/queryCoopOrgList.do",1,"mybody2","paging_bar_2");
				}
			},
		 error : function(XMLHttpRequest, textStatus) {
				msg.alert("错误", "错误代码：" + XMLHttpRequest.status + ",错误描述："+ textStatus, 'error');
			}
	});
}


function saveAgentOrgMer(){
	var agentOrgMer="";
	$("input[name='mybody2paging_bar_2']").each(function(){
		if(this.checked==true){
			agentOrgMer+=$(this).val()+",";
		}
	});
	agentOrgMer = agentOrgMer.substring(0, agentOrgMer.lastIndexOf(","));
	if(agentOrgMer.length<=0){
		alertMsg.error('请选择需要绑定的大商户号');
		return false;
	}
	
	$.ajax({
		 type:"post",
		 dataType:"json",
		 async:false,
		 url:"mpamng/agent/saveAgentOrg.do",
		 data: {agentOrgMer:agentOrgMer,agentId:$("#agentId").val()},
		 success : function(result) {
				if (result.rspcod != 200) {
					msg.alert("错误", result.rspmsg + " 错误代码："+ result.rspcod, 'error');
				} else {
					getjson("mpamng/agent/queryAgentOrgList.do",1,"mybody1","paging_bar_1");
					getjson("mpcoop/cooporgmer/queryCoopOrgList.do",1,"mybody2","paging_bar_2");
				}
			},
		 error : function(XMLHttpRequest, textStatus) {
				msg.alert("错误", "错误代码：" + XMLHttpRequest.status + ",错误描述："+ textStatus, 'error');
			}
	});
}
</script>
